<?php
/**
 * @file Appleseed.class.php
 * @author LD <2009-10-29 09:36:21>
 * @package kbao
 * @desc 
 *  交易平台登录验证,安全核心
 * 		-- 《 苹果核战记》
 */
if(!defined('ACCOUNT_INIT')) 
  exit('Access Denied!');

 /**
  * 函数连接数据库静态对象, 验证IM/网站登录状态;
  *
  */
class Appleseed{
	private static $instance = NULL; //MySQLi静态对象
	public $mysqli;
	public $blackHole = null;

	private function __construct(){}
	public function __destruct(){
		if (self::$instance->mysqli)
			self::$instance->mysqli->close();
	}
	
	/**
	 * 获取MySQLi静态对象
	 * @return object
	 */
	public static function getInstance(){
		if (self::$instance == NULL){
			$startTime = microtime();
			self::$instance = new Appleseed();
			self::$instance->mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
			if (!self::$instance->mysqli) {
				die('Could not connect: ' . mysqli_connect_error());
			}
			/*if ( !self::$instance->mysqli->select_db(DB_NAME) ) {
				die('Could not select database: '.mysqli_error());
			}
			self::$instance->mysqli->query("SET NAMES ".DB_CHARSET);*/
			$endTime = microtime();
			self::$instance->blackHole = urlencode( $startTime.md5($startTime - $endTime).$endTime );
		}
		return self::$instance;
	}//END func getInstance
	
	/**
	 * 获得BlackHole
	 * @return String
	 */
	public static function getBlackHole(){
		return self::getInstance()->blackHole;
	}//END func getBlackBole

	/**
	 * 执行输入的SQL语句
	 *
	 * @param String $query
	 * @return mix
	 */
	public static function executeSQL($query){
		$mysqli = self::getInstance()->mysqli;
		$re = $mysqli->query($query);
		
		if (is_object($re)){
			$row = $re->fetch_assoc();
			$mysqli->next_result();
		}
		
		return $row;
	}//END func executeSQL
	
	/**
	 * 校验K币交易滴安全密码
	 *
	 * @param mix $user_id
	 * @param String $passwd
	 * @return mix
	 *   result 结果 1成功, -1失败
	 */
	public static function chkBurzaLogin($user_id,$passwd){
		$ip = getIP();
		$query = "CALL ".DB_NAME_KBAO.".sp_chkBurzaLogin({$user_id},'{$passwd}','{$ip}','kbao-pass')";
		$re = self::executeSQL($query);
		
		return $re['result'];
	}//END func chkBurzaLogin
	
	/**
	 * 修改密码
	 *
	 * @param mix $user_id
	 * @param String $passwd
	 * @param String $pswd
	 * @param String $oldpswd
	 * @return mix
	 *   result
	 * 		1 密码修改成功
	 * 		0 参数错误
	 * 		-1 登陆密码验证失败
	 * 		-2 K币交易系统安全密码验证失败
	 * 		2 密码修改失败
	 */
	public static function chgBurzaPasswd($user_id, $passwd, $pswd, $oldpswd){
		$ip = getIP();
		$query = "CALL ".DB_NAME_KBAO.".sp_chgBurzaPasswd({$user_id},'{$passwd}','{$pswd}','{$oldpswd}','{$ip}','kbao-pass')";
		$re = self::executeSQL($query);
		
		return $re['result'];
	}//END func chgBurzaPasswd
	
	/**
	 * 是否设置了安全密码
	 * @return int
	 *   -1 系统错误
	 *   0 没有设置
	 *   1 已经设置
	 *   2 用户不存在
	 */
	public static function isSetPasswd($user_id){
		$query = "SELECT user_id, passwd FROM ".DB_NAME_KBAO.".bus_user WHERE user_id = {$user_id}";
		$mysqli = self::getInstance()->mysqli;
		
		$re = $mysqli->query($query);
		$row = $re->fetch_assoc();
		$mysqli->next_result();
		
		if ( empty($row['passwd']) ){
			return 0;
		}else {
			return 1;
		}
	}//END func isSetPasswd
	
	/**
	 * 得到交易列表
	 *
	 * @param mix $user_id
	 * @param mix $bid
	 * @param mix $type
	 * @param mix $offset
	 * @return Array
	 */
	public static function getTrustList($user_id, $bid, $type, $offset){
		$query = "CALL bus_getlist({$user_id}, {$bid}, {$type}, {$offset})";
		$mysqli = self::getInstance()->mysqli;
		
		$re = $mysqli->query($query);
		$result = array();
		while ($row = $re->fetch_assoc()){
			array_push($result, $row);
		}
		$mysqli->next_result();
		
		return $result;
	}//END func getTrustList
	
	/**
	 * 得到用户基本信息(主账户K币、K宝余额; 交易账户K币、K宝余额)
	 *
	 * @param mix $user_id
	 * @return array
	 */
	public static function getUserInfo($user_id){
		$query = "CALL ".DB_NAME_KBAO.".bus_info({$user_id})";
		$mysqli = self::getInstance()->mysqli;
		
		$re = $mysqli->query($query);
		$row = $re->fetch_assoc();
		$mysqli->next_result();
		
		return $row;
	}//END func getUserInfo
	
	/**
	 * 得到Top10
	 * @return array
	 */
	public static function getTop10(){
		$query = "CALL ".DB_NAME_KBAO.".bus_top()";
		$mysqli = self::getInstance()->mysqli;
		
		$re = $mysqli->query($query);
		$result = array();
		while ($row = $re->fetch_assoc()){
			array_push($result, $row);
		}
		$mysqli->next_result();
		
		return $result;		
	}//END func getTop10
	
	/**
	 * 得到K币交易系统的内置参数
	 * @return array
	 */
	public static function getOptions(){
		$query = "CALL ".DB_NAME_KBAO.".bus_option();";
		$mysqli = self::getInstance()->mysqli;
		
		$re = $mysqli->query($query);
		while ($row = $re->fetch_assoc()){
			$resut[$row['name']] = $row['value'];
		}
		$mysqli->next_result();
		
		return $resut;
	}//END func getOptions
	
	/**
	 * 验证是否通过登陆
	 *
	 * @param mix $user_id
	 * @param String $sessionKey
	 * @return array
	 */
	public static function isLogin($user_id, $sessionKey = ''){
		$login = false;
		
		if (!empty($sessionKey)){ //验证 sessionKey 
			$ip = getIP();
			$query = "CALL ".DB_NAME_IM
			  .".sp_cklogin('{$user_id}','{$sessionKey}','session','{$ip}','kbao')";
			$mysqli = self::getInstance()->mysqli; 
			
			$re = $mysqli->query($query);
			if (is_object($re)){
				$row = $re->fetch_assoc();
				$_SESSION['account_arr'] = $row;
				$mysqli->next_result();
			}
		}
			// 验证 session
		if ( $user_id == $_SESSION['account_arr']['chk_uin']
		  and !empty($_SESSION['account_arr']['chk_nick']) ){
			$login = true;
			$uin = $_SESSION['account_arr']['chk_uin'];
			$nick = $_SESSION['account_arr']['chk_nick'];
		}
			// 编码转换
		if ($login){
			$nick = iconv ( 'GB2312', 'UTF-8//TRANSLIT', $nick );
			if ( mb_strlen($nick, 'UTF-8') > 5 ) {
				$snick = cut_str($nick, 5, 0, 'UTF-8');
			}else $snick = $nick;
		}
		
		return array ('result' => $login, 'user_id' => $uin, 
		  'user_nick' => $nick, 'user_snick' => $snick );
	}//END func isLogin
	
}//END class Appleseed
?>